<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Web
 * @version $Id$
 */

/**	
 * An Abstract Controller 
 *
 * This abstract controller offers some basic functions e.g. forward or redirect a request 
 * to another controller, hooks like before and after handle etc.
 * 
 * @package auxo
 * @subpackage Web
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
abstract class tx_auxo_AbstractController implements tx_auxo_ControllerInterface {
	
   /**
    * Initializes the controller instance 
    *
    * @return void
    */
	public function initialize() {
	}
	
   /**
    * Method is called before execution of handle request
    *
    * @return void
    */
	public function beforeExecute() {	
	}
	
   /**
    * Method is called after execution of handle request
    *
    * @return void
    */
	public function afterExecute() {
	}
	
	/**
	 * Handles a request and returns ususally a model view object
	 *
	 * @param tx_auxo_Request $request
	 * @param tx_auxo_Response $response
	 * @param mixed $result 
	 */
	abstract public function handleRequest(tx_auxo_Request $request, tx_auxo_Response $response);
	
    /**
     * Forward to another controller 
     *
     * @param string $target target string either: controller[/action] or route
     * @return void
     */
	public function forward($target) {
		if ($target[0] == '@') {
			/**
			 *  @TODO implementation of routing with symbolic names 
			 */
		}
		else {
			$module = $target['component'] ? $target['component'] : $this->get('module');
			$action = $target['action'];
			if (!$action) {
				throw new tx_auxo_InvalidArgumentException('Controller wants to forward: no action given');
			}
		}
				 			
		throw new tx_auxo_ControllerForwardException($component, $action);
	}
	
  /**
   * Redirects to an URL or page Id
   *
   * @param mixed $destination
   * @return void
   */
	public function redirect($destination) {
		$link = tx_div::makeInstance('tx_lib_link');
		if (strpos($destination, '//') === true)  {
			$link->destination($pageId);
	   	    $link->redirect($link->makeUrl());
		}
		else {
   	    	$link->redirect($url);			
		}
		throw tx_auxo_actionException( tx_auxo_actionException::STOP_ACTION );			
	}
		
   /**
    * Redirects to an URL if $condition is true
    *
    * @param mixed $url
    * @param mixed $condition
    * @return void
    */
	public function redirectIf($url, $condition) {
		if ($condition == true) {
			$this->redirect($url);
		}
	}

   /**
    * Redirects to an URL if $condition is false
    *
    * @param mixed $url
    * @param mixed $condition
    * @return void
    */
	public function redirectUnless($url, $condition) {
		if ($condition == false) {
			$this->redirect($url);
		}
	}
}

?>